<?php

function generateMatrixRepresentation($ruleData, $iPlayer) {

    $content = "<table class=\"rules_matrix_content\">\n<tr>\n<td></td>\n<td><div id=\"player_label2\" " . ($iPlayer == 2 ? "style=\"font-weight:bold;\"" : "") . ">" . $ruleData->getPlayerLabel(1) . "</div></td>\n</tr>\n<tr>\n<td><div id=\"player_label1\" " . ($iPlayer == 1 ? "style=\"font-weight:bold;\"" : "") . ">" . $ruleData->getPlayerLabel(0) . "</div></td>\n<td>\n";

    //<img class=\"option_img\" src=\"/images/iPlayer2.png\">
    //<img class=\"option_img\" src=\"/images/iPlayer1.png\">
    $content.="<table class=\"rules_matrix_table\">\n<tr>\n<td></td>\n";

    $ri = 1;
    foreach ($ruleData->getRows() as $row) {
        $content.="<td colspan=\"2\" class=\"matrix_actions2 game_option\">" . ($iPlayer == 2 ? "<a href=\"/send-option/" . $ri . "\" class=\"ingame_action\" title=\"Wybierz akcję " . $row . "\"><div><img class=\"option_img\" src=\"/images/iAction.png\">" . $row . "</a></div>" : $row) . "</td>\n";
        $ri++;
    }
    //<img class=\"option_img\" src=\"/images/iAction.png\"> 
    $content.="</tr>\n";
    $size = $ruleData->getSize();
    $j = 0;

    $ci = 1;
    foreach ($ruleData->getCols() as $col) {
        $content.="<tr class=\"matrix_row\">\n";
        $content.="<td class=\"matrix_actions1 matrix_row \">" . ($iPlayer == 1 ? "<a href=\"/send-option/" . $ci . "\" class=\"ingame_action\" title=\"Wybierz akcję " . $col . "\"><div><img class=\"option_img\" src=\"/images/iAction.png\">" . $col . "</a></div>" : $col) . "</td>\n";
        for ($i = 0; $i < $size[0]; $i++) {
            $cell = $ruleData->getCell($i, $j);
            $content.="<td class=\"matrix_cell1 matrix_row\"><span class=\"cell_points1\">" . $cell[0] . "</span></td>\n";
            $content.="<td class=\"matrix_cell2 matrix_row\"><span class=\"cell_points2\">" . $cell[1] . "</span></td>\n";
        }
        $ci++;
        $j++;
        $content.="</tr>\n";
    }
    $content.="</table>\n</td>\n</tr>\n</table>";
    return $content;
}

function generateNode2(&$a, $i, $act, $iPlayer) { // &a tablica drzewa, $i index tablicy wezlow, $act ID wezla aktualnego(nie index!), $iPlayer 1 lub 2 0 dla nikogo
    $content = '';
    $content.='<table id="tree_node' . $a[$i]->getId() . '" class="tree_preview_' . ($a[$i]->getType() == 3 ? 'points2' : 'table2') . '">';
    $content.=generateNodeLabel2($a, $i, $act, $iPlayer);
    $content.=generateNodeActions2($a, $i, $act, $iPlayer);
    $content.="</table>";
    return $content;
}

function getLines(&$a, $i) {
    $j = $a[$i]->getActions()->count();
    switch ($j) {
        case 1:
            return '<tr><td><div style="height:40px;overflow:visible;position:relative;top:-4px;"><img src="/images/lines1.png" width="1" height="64"/></div></td></tr>';
            break;
        case 2:
            return '<tr><td colspan="' . $j . '"><div style="height:40px;overflow:visible;position:relative;top:-4px;"><img src="/images/lines2.png" width="50%" height="64"/></div></td></tr>';
            break;
        case 3:
            return '<tr><td colspan="' . $j . '"><div style="height:40px;overflow:visible;position:relative;top:-4px;"><img src="/images/lines3.png" width="66%" height="64"/></div></td></tr>';
            break;
        case 4:
            return '<tr><td colspan="' . $j . '"><div style="height:40px;overflow:visible;position:relative;top:-4px;"><img src="/images/lines4.png" width="75%" height="64"/></div></td></tr>';
            break;
        case 5:
            return '<tr><td colspan="' . $j . '"><div style="height:40px;overflow:visible;position:relative;top:-4px;"><img src="/images/lines5.png" width="80%" height="64"/></div></td></tr>';
            break;
        case 6:
            return '<tr><td colspan="' . $j . '"><div style="height:40px;overflow:visible;position:relative;top:-4px;"><img src="/images/lines6.png" width="84%" height="64"/></div></td></tr>';
            break;
    }
}

function generateNodeLabel2(&$a, $i, $act, $iPlayer) {
    
    $group = -1;
    if ($a[$i]->getType() == 0 || $a[$i]->getType() == 1) {
        foreach ($a as $v) {
            if ($act == $v->getId() && ($v->getType() == 0 || $v->getType() == 1)) {
                $group = $v->getGroup();
                break;
            }
        }
    } 

    switch ($a[$i]->getType()) {
        case 0:
            return '<tr id="tree_node' . $a[$i]->getId() . 'label"><td colspan="' . $a[$i]->getActions()->count() . '"><div class="tree_preview_name2" title="' . $a[$i]->getLabel() . ' grupa ' . $a[$i]->getGroup() . '" style="background-color:' . GameRules::getGroupColor($a[$i]->getGroup()) . ';' . ($group == $a[$i]->getGroup() && $iPlayer == 1 ? 'border:2px solid black; margin:0px auto;color:black' : '') . '">1</div>' . ($group == $a[$i]->getGroup() && $iPlayer == 1 ? 'Wybierz akcję' : '') . '</td></tr>' . getLines($a, $i);
            break; //<img class="option_img" src="/images/iPlayer1.png">
        case 1:
            return '<tr id="tree_node' . $a[$i]->getId() . 'label"><td colspan="' . $a[$i]->getActions()->count() . '"><div class="tree_preview_name2" title="' . $a[$i]->getLabel() . ' grupa ' . $a[$i]->getGroup() . '" style="background-color:' . GameRules::getGroupColor($a[$i]->getGroup()) . ';' . ($group == $a[$i]->getGroup() && $iPlayer == 2 ? 'border:2px solid black; margin:0px auto;color:black' : '') . '">2</div>' . ($group == $a[$i]->getGroup() && $iPlayer == 2 ? 'Wybierz akcję' : '') . '</td></tr>' . getLines($a, $i);
            break; //<img class="option_img" src="/images/iPlayer2.png">                    
        case 2:
            return '<tr id="tree_node' . $a[$i]->getId() . 'label"><td colspan="' . $a[$i]->getActions()->count() . '"><div class="tree_preview_name2" title="Natura" ' . ($act == $a[$i]->getId() && $iPlayer != 0 ? 'style="border:2px solid black; margin:0px auto;color:black;"' : '') . '>N</div></td></tr>' . getLines($a, $i);
            break; //<img class="option_img" src="/images/iNature.png">
        case 3:
            return '';
            break;
    }
}

function getIdIndex2(&$a, $id) {

    for ($n = 0; $n < count($a); $n++) {
        if ($id == $a[$n]->GetId()) {
            return $n;
        }
    }
}

function generateNodeActions2(&$a, $i, $act, $iPlayer) {
    
    $group = -1;
    
    if ($a[$i]->getType() == 0 || $a[$i]->getType() == 1) {
        foreach ($a as $v) {
            if ($act == $v->getId() && ($v->getType() == 0 || $v->getType() == 1) ) {
                $group = $v->getGroup();
                break;
            }
        }
    } 


    $content = '';
    $content.='<tr id="tree_node' . $a[$i]->getId() . 'actions">';

    $node = $a[$i];




    switch ($node->getType()) {
        case 0:
            for ($x = 0; $x < $node->getActions()->count(); $x++) {
                $action1 = $node->getActions()->getRaw($x);
                $content.='<td class="tree_preview_td2" id="tree_node' . $node->getId() . 'action' . $action1[1] . '" >' . ($group == $a[$i]->getGroup() && $iPlayer == 1 ? '<a href="/send-option-tree/' . ($x + 1) . '" class="ingame_tree_action"><span title="Wybierz akcję ' . $action1[0] . '"><img class="option_img" src="/images/iAction.png">' . $action1[0] . '</span></a>' : '<span class="ingame_tree_action" style="font-size: 13px;font-weight:bold;color:#2a2a2a;">' . $action1[0] . '</span>') . generateNode2($a, getIdIndex2($a, $action1[1]), $act, $iPlayer) . '</td>';
            }
            $content.='</tr>';
            break;

        case 1:
            for ($x = 0; $x < $node->getActions()->count(); $x++) {
                $action1 = $node->getActions()->getRaw($x);
                $content.='<td class="tree_preview_td2" id="tree_node' . $node->getId() . 'action' . $action1[1] . '" >' . ($group == $a[$i]->getGroup() && $iPlayer == 2 ? '<a href="/send-option-tree/' . ($x + 1) . '" class="ingame_tree_action"><span title="Wybierz akcję ' . $action1[0] . '"><img class="option_img" src="/images/iAction.png">' . $action1[0] . '</span></a>' : '<span class="ingame_tree_action" style="font-size: 13px;font-weight:bold;color:#2a2a2a;">' . $action1[0] . '</span>') . generateNode2($a, getIdIndex2($a, $action1[1]), $act, $iPlayer) . '</td>';
            }
            $content.='</tr>';
            break;

        case 2:
            for ($x = 0; $x < $node->getActions()->count(); $x++) {
                $action2 = $node->getActions()->getRaw($x);
                $content.='<td class="tree_preview_td2" id="tree_node' . $node->getId() . 'action' . $action2[2] . '"><span class="ingame_tree_action">' . $action2[0] . '</span> <span class="ingame_tree_prob">(' . $action2[1] . ')</span>' . generateNode2($a, getIdIndex2($a, $action2[2]), $act, $iPlayer) . '</td>';
            }
            $content.='</tr>';
            break;

        case 3:
            $content.='<td class="tree_preview_td2" id="tree_node' . $node->getId() . 'points">(' . $node->getPlayerPoints(0) . ',' . $node->getPlayerPoints(1) . ')</td>';
            $content.='</tr>';
            break;
    }

    return $content;
}

?>